package com.imooc.service.impl;

import com.imooc.pojo.ItemsIk;
import com.imooc.service.ItemsESService;
import com.imooc.utils.PagedGridResult;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2022-04-30 0:37
 */
@Service
public class ItemsESServiceImpl implements ItemsESService {
    @Autowired
    private ElasticsearchTemplate esTemplate;
    @Override
    public PagedGridResult searchItems(String keywords, String sort, Integer page, Integer pageSize) {
        String preTag="<font color='red'>";
        String postTag="</font>";
        Pageable pageable = PageRequest.of(page,pageSize);

        SortBuilder sortBuilder = null;
        if (sort.equals("c")) {
            sortBuilder = new FieldSortBuilder("sellCounts")
                    .order(SortOrder.DESC);
        } else if (sort.equals("p")) {
            sortBuilder = new FieldSortBuilder("price")
                    .order(SortOrder.ASC);
        } else {
            sortBuilder = new FieldSortBuilder("itemName.keyword")
                    .order(SortOrder.ASC);
        }

        String itemNameFiled="itemName";

        SearchQuery query=new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery(itemNameFiled,keywords))
                .withHighlightFields(new HighlightBuilder.Field(itemNameFiled)
                        //.preTags(preTag)
                        //.postTags(postTag)
                        )
                .withSort(sortBuilder)
                //.withSort(sortBuilderAge)
                .withPageable(pageable)
                .build();
        AggregatedPage<ItemsIk> pagedItems =esTemplate.queryForPage(query, ItemsIk.class, new SearchResultMapper() {
            @Override
            public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> aClass, Pageable pageable) {
                List<ItemsIk> itemHighlightList=new ArrayList<>();
                SearchHits hits = response.getHits();
                for(SearchHit h:hits){
                    HighlightField highlightField = h.getHighlightFields().get(itemNameFiled);
                    String itemName = highlightField.getFragments()[0].toString();

                    String itemId=(String)h.getSourceAsMap().get("itemId");
                    String imgUrl=(String)h.getSourceAsMap().get("imgUrl");
                    Integer price=(Integer)h.getSourceAsMap().get("price");
                    Integer sellCounts=(Integer)h.getSourceAsMap().get("sellCounts");



                    ItemsIk item=new ItemsIk();
                    item.setItemId(itemId);
                    item.setItemName(itemName);
                    item.setImgUrl(imgUrl);
                    item.setPrice(price);
                    item.setSellCounts(sellCounts);

                    itemHighlightList.add(item);
                }

                return new AggregatedPageImpl<>((List<T>)itemHighlightList,
                                                pageable,
                                                response.getHits().totalHits);
            }
        });

        //System.out.println(pagedItems.getTotalPages());
        //List<ItemsIk> stuList= pagedItems.getContent();
        //for(ItemsIk s:stuList){
        //    System.out.println(s);
        //}

        PagedGridResult gridResult=new PagedGridResult();
        gridResult.setRows(pagedItems.getContent());
        gridResult.setPage(page+1);
        gridResult.setTotal(pagedItems.getTotalPages());
        gridResult.setRecords(pagedItems.getTotalElements());
        return gridResult;
    }
}
